Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue311 improve hook searching #613

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from

Conversation

steniobhz
Copy link
Collaborator

@steniobhz steniobhz commented Oct 4, 2024

Documentation for the Hook Search API (Listeners and Routes)

Available Endpoints

  1. Search Listeners
  • Endpoint: /registrations/listeners
  • HTTP Method: GET
  • Description: Searches for all Listeners whose destination field contains the provided query string in the q parameter.
  1. Search Routes
  • Endpoint: /registrations/routes
  • HTTP Method: GET
  • Description: Searches for all Routes whose destination field contains the provided query string in the q parameter.

Query Parameter

Both endpoints support the q query parameter, which is used to filter the search results.

  • q: A string to search for within the destination field of Listeners or Routes.

Example Request:
GET /registrations/listeners?q=myDestination
GET /registrations/routes?q=myDestination

Response Structure
Example Response - Searching for Listeners:

{
    "listeners": [
        "listener1",
        "listener2",
        "listener3"
    ]
}

Example Response - Searching for Routes:

{
    "routes": [
        "route1",
        "route2",
        "route3"
    ]
}

Key Features

  1. Separate Endpoints for Listeners and Routes: The API provides distinct endpoints for Listeners and Routes searches, ensuring optimized search results based on specific hook types.

  2. String-Based Search: The API looks for matches in the destination field of both Listeners and Routes. It searches for any substring match rather than an exact match, making the search more flexible.

  3. Optimized Search Logic: The API uses efficient search mechanisms internally to quickly find results across registered listeners or routes, reducing response time even with large datasets.

Example Workflow

  1. A client wants to find all Listeners with a destination that contains the string "myDestination". They make the following request:
    GET /registrations/listeners?q=myDestination

  2. The API returns a JSON object containing the identifiers of matching Listeners:

{
    "listeners": [
        "listener1",
        "listener2"
    ]
}
  1. If no Listeners match the query, the API responds with a 404 error:
{
    "error": "No listeners/routes found matching the query."
}

Copy link
Collaborator

@mcweba mcweba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed on the phone, you should also add some integration tests in the gateleen-test module. Where is the documentation of this new API feature?

Add new tests for HookHandler
Create testes with storage
- Verifies successful listener search when multiple listeners are present in storage.
- Ensures correct retrieval of a single listener from storage.
- Tests failure case when searching for a non-existent listener among multiple listeners.
- Ensures proper handling of search when no listeners are registered in storage.
…istener is found for a given query parameter.
…eners registered.

- Test for handling searches with no matching listeners, returning an empty list.
- Test for handling searches when no listeners are registered, ensuring an empty list is returned.
- Fix hookHandler Search
…tory. It checks if the destination of each item contains the query string (queryParam), adding matching items to the result.
- improve listeners tests
Copy link

codecov bot commented Oct 17, 2024

Codecov Report

Attention: Patch coverage is 54.28571% with 16 lines in your changes missing coverage. Please review.

Project coverage is 48.87%. Comparing base (45f39c1) to head (4fdab6c).
Report is 23 commits behind head on develop.

Files with missing lines Patch % Lines
.../java/org/swisspush/gateleen/hook/HookHandler.java 54.28% 14 Missing and 2 partials ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             develop     #613      +/-   ##
=============================================
+ Coverage      48.44%   48.87%   +0.42%     
- Complexity      1862     1892      +30     
=============================================
  Files            238      238              
  Lines          11953    12049      +96     
  Branches        1261     1278      +17     
=============================================
+ Hits            5791     5889      +98     
+ Misses          5659     5653       -6     
- Partials         503      507       +4     
Flag Coverage Δ
?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@steniobhz steniobhz force-pushed the Issue311_Improve-hook-searching branch from 4fdab6c to 4004646 Compare October 17, 2024 12:49
@steniobhz steniobhz requested a review from mcweba October 17, 2024 12:54

```
GET http://myserver:7012/gateleen/server/listenertest/_hooks/listeners/listener/1?q=testQuery
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how listener hooks are searched. The URL does not contain the listener-ID.

```json
{
"listeners": [
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The content of searching listeners and routes is not an array of objects (with destination, methods properties) but an array containing a list of names (strings). How did you even get this response?

private static final String CONTENT_TYPE_JSON = "application/json";
private static final String LISTENERS_KEY = "listeners";
private static final String ROUTES_KEY = "routes";
private static final String DESTINATION_KEY = "destination";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your newly created DESTINATION_KEY static variable is not used

@@ -569,6 +577,22 @@ public boolean handle(final RoutingContext ctx) {
}
}

// 1. Check if the request method is GET
if (request.method() == HttpMethod.GET) {
String uri = request.uri();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract var requestUri = request.uri(); only once in the handle method. It is already done for PUTand DELETE requests. We should optimize this.

// 1. Check if the request method is GET
if (request.method() == HttpMethod.GET) {
String uri = request.uri();
String queryParam = request.getParam("q");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using any other query params than q in GET requests should result in a 400 Bad Request and not just return all listeners/hooks. Also create tests to verify this.


// Register a route with a different query param
String differentQueryParam = "differentQuery";
TestUtils.registerRoute(requestUrlBase + routePath + "?q=" + differentQueryParam, targetUrlBase, new String[]{"GET", "POST"}, null, true, true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Query param in PUT request for route registration. This does not work

TestUtils.registerRoute(requestUrlBase + routePath + "?q=" + differentQueryParam, targetUrlBase, new String[]{"GET", "POST"}, null, true, true);

// Send GET request with non-matching query param
given().queryParam("q", nonMatchingQueryParam)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalid uri

.body("routes", Matchers.empty());

// Validate response
checkGETStatusCodeWithAwait(requestUrl, 200);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify response

// No routes registered

// Send GET request with a query param
given().queryParam("q", queryParam)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalid uri

.body("routes", Matchers.empty());

// Validate response
checkGETStatusCodeWithAwait(requestUrl, 200);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants